//
//  MSDetailedEvaluationController.m
//  觅尚
//
//  Created by No.8 on 2019/4/3.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSDetailedEvaluationController.h"
#import "MSDetaileEvaluationCell.h"
#import "MSDetailedEvaluationModel.h"
#define pageSize 10

@interface MSDetailedEvaluationController ()<UITableViewDataSource,UITableViewDelegate>
{
    CustomNavigationBar *naviagtionBar;
    
    NSInteger page;
    
    NSInteger count;
}
/** <#name#>*/
@property(nonatomic,strong)NSMutableArray *listArray;

/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;

/** 考勤信息*/
@property(nonatomic,strong)UIImageView *CheckIMG;


/** 表头*/
@property(nonatomic,strong)UIView * HeaderView;


/** 底部所有的标签 */
@property (nonatomic, strong) UIView  * titlesView;


/** 底部所有的内容 */
@property (nonatomic, strong) UIScrollView  * contenView;

@end

@implementation MSDetailedEvaluationController

- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}

- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.view.backgroundColor = backgroundcolor;
    
    [self CreateNavigationBar];
    
    
    [self CreatView];
    
    
    [self tableView];
    
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}
- (void)FootderRefreshReloadData{
    [self.listArray removeAllObjects];
    NSDictionary * params = @{@"belongBusid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSEvaluaIncomeInfo params:params succeed:^(id responseObject) {
        if (CodeStr) {
                [self.tableView.mj_footer endRefreshing];
                self.listArray = [MSDetailedEvaluationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"rewardList"]];
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
             page--;
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
    
}
- (void)ReloadData{
    [self.listArray removeAllObjects];
    NSDictionary * params = @{@"belongBusid":[WYCAccountTool unarchivebelongBusid],@"uid":[WYCAccountTool unarchiveuid],@"page":@(page),@"pageSize":@(pageSize)};
    [HTTPRequest POST:MSEvaluaIncomeInfo params:params succeed:^(id responseObject) {
        WYCLog(@"----------%@",responseObject);
        if (CodeStr) {
            count = [responseObject[@"data"][@"count"] integerValue];
            [self.tableView.mj_header endRefreshing];
//            self.listArray = [MSDetailedEvaluationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            NSMutableArray *temporaryArray = [MSDetailedEvaluationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"rewardList"]];
            [temporaryArray addObjectsFromArray:self.listArray];
            self.listArray = temporaryArray;
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager showBriefAlert:Failed];
    }];
    
}
- (void)CreatView{
    // 标签栏整体
    UIView *titlesView = [[UIView alloc] init];
    titlesView.backgroundColor = BackViewColor;
    titlesView.frame = CGRectMake(0, [self navBarBottom], ScreenWidth, 100 * px);
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;
    
    
    NSArray * titleArray = @[@"日期",@"订房人",@"金额",@"我的收益"];
    
    
    CGFloat TitleLabelH = titlesView.height;
    CGFloat TitleLabelY = 0;
    CGFloat TitleLabelW = ScreenWidth / titleArray.count;
    
    for (int i = 0; i < titleArray.count; i ++) {
        /** 标题*/
        UILabel * TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(TitleLabelW * i, TitleLabelY, TitleLabelW, TitleLabelH)];
        TitleLabel.textColor = [UIColor grayColor];
        TitleLabel.text = titleArray[i];
        TitleLabel.textAlignment = NSTextAlignmentCenter;
        TitleLabel.font = [UIFont systemFontOfSize:SYRealValue(28 / 2)];
        [titlesView addSubview:TitleLabel];
    }
    
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom] + 100 * px, ScreenWidth, ScreenHeight - [self navBarBottom] - 100 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSDetaileEvaluationCell * cell = [MSDetaileEvaluationCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(self.listArray.count > indexPath.row){
        cell.DetailedEvaluationModel = self.listArray[indexPath.row];
    }
    
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"评价详情";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}
@end
